package br.edu.ufcg.dsc.actions.resource;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.config.Result;
import org.apache.struts2.dispatcher.ServletDispatcherResult;

import br.edu.ufcg.dsc.domain.Resource;
import br.edu.ufcg.dsc.domain.ResourceType;
import br.edu.ufcg.dsc.domain.resource.Article;
import br.edu.ufcg.dsc.services.ResourceService;

import com.opensymphony.xwork2.Preparable;

@Result(name = "success", type = ServletDispatcherResult.class, value = "/jsp/resource/article.jsp")
public class FindArticleAction extends ResourceDriven<Article> implements Preparable{

	private int resourceId;
	private Article article;
	private static final long serialVersionUID = 1L;

	public void prepare() throws Exception {
		Resource resource = ResourceService.find(resourceId);
		if (resource != null)
			article = new Article(resource.getMarc());
		else article = new Article();
	}

	public int getResourceId() {
		return resourceId;
	}

	public void setResourceId(int resourceId) {
		this.resourceId = resourceId;
	}

	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public Article createModel() {
		return article;
	}

	public String execute(){		
		return SUCCESS;
	}

	public List<Resource> getArticleOptions(String top){
		List<Resource> result = new ArrayList<Resource>();
		List<String> articlesOptions = new ArrayList<String>();
		if (top.equals("ISSUE")){
			articlesOptions.clear();
			result = ResourceService.findByType(ResourceType.ISSUE.ordinal());
		}
		else {
			articlesOptions.clear();
			result = ResourceService.findByType(ResourceType.ARTICLES_COLLECTION.ordinal());
		}
		return result;
	}
}
